<template>
  <el-cascader
    ref="domRef"
    v-model="formData[_info.field]"
    :clearable="_rules.required ? false : true"
    :filterable="_rules.filterable || false"
    :placeholder="_info.placeholder || '请选择'"
    :options="optionList"
    :props="cascaderProps"
    :show-all-levels="_rules.show_all_levels || false"
    :disabled="_info.disabled || false"
    @change="handleChange"
  />
  <div
    class="loading-dom"
    v-if="loading"
    v-loading="true"
    :element-loading-spinner="loadingSpinner"
    element-loading-svg-view-box="0,0, 1024, 1024"
  ></div>
</template>

<script setup>
import {
  getInfo,
  getRules,
  getCascaderProps,
  changeSelect,
  getOptions
} from '../const';
import { loadingSpinner } from '@/assets/js/loadingIcon.js';

const props = defineProps({
  formInfo: {
    type: Object,
    default: () => {}
  },
  formData: {
    type: Object,
    default: () => {}
  },
  TrendsFormRef: {
    type: Object,
    default: null
  },
  topExpose: {
    type: Object,
    default: () => {}
  }
});

// 常量
const _info = computed(() => getInfo(props.formInfo));
const _rules = computed(() => getRules(props.formInfo));
const cascaderProps = computed(() => getCascaderProps(props.formInfo));

// 拿到下拉列表
const optionList = ref([]);
const loading = ref(false);
getOptions({ optionList, loading, props });

let oldValue = undefined;
watch(
  () => props.formData[_info.value.field],
  (newVal, oldVal) => (oldValue = oldVal),
  { deep: true, immediate: true }
);

// 处理change事件
const domRef = ref();
function handleChange() {
  setTimeout(() => {
    changeSelect(props.formInfo, props.formData, {
      oldValue,
      domRef,
      topExpose: props.topExpose
    });
  }, 0);
}
</script>

<style lang="scss" scoped>
.loading-dom {
  position: absolute;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>
